package com.telchina.workorder.controller;

import cn.hutool.core.lang.UUID;
import com.telchina.common.annotation.Log;
import com.telchina.common.core.controller.BaseController;
import com.telchina.common.core.domain.AjaxResult;
import com.telchina.common.core.domain.entity.SysOrg;
import com.telchina.common.enums.BusinessType;
import com.telchina.common.utils.DateUtils;
import com.telchina.common.utils.SecurityUtils;
import com.telchina.common.utils.poi.ExcelUtil;
import com.telchina.workorder.domain.WoReply;
import com.telchina.workorder.domain.vo.WoReplyVO;
import com.telchina.workorder.service.IWoReplyService;
import com.telchina.workorder.third.service.SystemThirdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 回复单Controller
 *
 * @author konghao
 * @date 2023-02-21
 */
@RestController
@RequestMapping("/workorder/woReply")
public class WoReplyController extends BaseController {
    @Autowired
    private IWoReplyService woReplyService;

    @Autowired
    private SystemThirdService thirdService;


    /**
     * 分页查询回复单列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:reply:list')")
    @GetMapping("/list")
    public AjaxResult list(WoReply woReply) {
        startPage();
//        IPage<WoReply> list = woReplyService.selectList(startPage(), woReply);
        List<WoReply> list = woReplyService.selectListAll(woReply);
        return getDataTable(list);
    }

    /**
     * 查询回复单列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:reply:list')")
    @GetMapping("/list/all")
    public AjaxResult listAll(WoReply woReply) {
        List<WoReplyVO> listVO = woReplyService.selectListAllAndAttachment(woReply);
        return AjaxResult.success(listVO);
    }

    /**
     * 导出回复单列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:reply:export')")
    @Log(title = "回复单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WoReply woReply) {
        List<WoReply> list = woReplyService.selectListAll(woReply);
        ExcelUtil<WoReply> util = new ExcelUtil<WoReply>(WoReply.class);
        return util.exportExcel(list, "reply");
    }

    /**
     * 获取回复单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('workorder:reply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(woReplyService.selectById(id));
    }

    /**
     * 新增回复单
     */
//    @PreAuthorize("@ss.hasPermi('workorder:reply:add')")
    @Log(title = "回复单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WoReply woReply) {
        return woReplyService.insert(woReply);
    }

    /**
     * 修改回复单
     */
//    @PreAuthorize("@ss.hasPermi('workorder:reply:edit')")
    @Log(title = "回复单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WoReply woReply) {
        return toAjax(woReplyService.update(woReply));
    }

    /**
     * 删除回复单
     */
//    @PreAuthorize("@ss.hasPermi('workorder:reply:remove')")
    @Log(title = "回复单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(woReplyService.deleteByIds(ids));
    }

    /**
     * 初始化是设置工单单号  受理时间  受理单号等信息
     */
//    @PreAuthorize("@ss.hasPermi('workorder:reply:add')")
    @GetMapping("/getInitReply")
    public AjaxResult getInitReplay() {
        Map<String, Object> result = new HashMap<>();
        // 获取登录人所在部门信息
        SysOrg sysOrg = thirdService.getLoginUseDeptByUserId(SecurityUtils.getUserId());
        result.put("id", UUID.fastUUID().toString(Boolean.TRUE));
        result.put("sysOrg", sysOrg);
        result.put("replyNo", DateUtils.parseDateToStr("yyMMdd", new Date()));
        return AjaxResult.success(result);
    }
}
